# 由于之前壁球小游戏的运动速度过快，所以我们得对这个游戏进行优化
# 屏幕的帧率设置

# 导入必要库
import pygame
import sys

# 初始化pygame
pygame.init()

# 初始化各种设置
size = width, height = 600, 400
speed = [1, 1]
BLACK = (0, 0, 0)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("壁球小游戏V2")
ball = pygame.image.load("ball.gif")
ballrect = ball.get_rect()

# 设置刷新率
fps = 200  # frames per second
fclock = pygame.time.Clock()  # 创建了一个Clock对象，用于操作时间


# 编写循环
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    ballrect = ballrect.move(speed[0], speed[1])
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    # 刷新屏幕
    screen.fill(BLACK)
    screen.blit(ball, ballrect)
    pygame.display.update()
    fclock.tick(fps)
